package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.RecordKit;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.commission.Commission;
import dshop.model.commission.CommissionDetail;

@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class CommissionAction  extends ApiBaseController {
	
	
	public void list()
	{
		String phone = getPara("phone","").trim();
		String username = getPara("username","").trim();
		
		String select = " select c.*, s.create_time, concat(m.last_name, m.first_name) as full_name, m.phone ";
		String where = " from sp_commission c  "
					 + " inner join member_general_info m on c.user_id = m.account_id " 
					 + " left join (select user_id,max(create_time) 'create_time' from sp_commission_detail  GROUP BY user_id )"
					 + " s on s.user_id = c.user_id ";
		
		List<Object> params = new ArrayList<Object>();
		
		if(!phone.isEmpty())
		{
			where += "and (m.phone like ? )";
			String keyWordParam = "%" + phone + "%";
			params.add(keyWordParam);
		}
		if(!username.isEmpty())
		{
			where += "and (concat(m.last_name, m.first_name) like ? )";
			String keyWordParam = "%" + username + "%";
			params.add(keyWordParam);
		}
		
		where += " ORDER BY s.create_time desc ";
		
		Page<Record> page = page(select,where,params.toArray());
		List<Record> list = page.getList();
		for (Record r : list) 
		{
			if (Commission.checkSign(r.getInt("id"), r.getInt("user_id"), r.getBigDecimal("money"), r.getInt("version"), r.getStr("sign")))
			{
				r.set("is_valid", AppConst.STATUS_VAL_YES);
			}else{
				r.set("is_valid", AppConst.STATUS_VAL_NO);
			}
		}
		
		renderSuccess();
	}
	
	
	public void detailList()
	{
		int userId = getParaToInt("userId");
		int changeType = getParaToInt("changeType",0);

		String select = "select id, user_id, money, money_before, money_after, descs, change_type, change_ref_id, create_time, sign ";
		String where = " from sp_commission_detail where user_id = ? ";
		String orderBy = " order by create_time desc, id desc ";
		
		List<Object> params = new ArrayList<Object>();
		params.add(userId);
		
		if(changeType > 0)
		{
			where += " and change_type = ? ";
			params.add(changeType);
		}
		
		Page<Record> page = page(select, where + orderBy, params.toArray());
		List<Record> list = page.getList();
		
		RecordKit.setAttrStrVal(list, Commission.changeTypeMap(), "change_type", "change_type_str");
		RecordKit.putDateStr(list, "create_time", "create_time_str", "yyyy-MM-dd HH:mm");
		
		for (Record r : list) 
		{
			if (CommissionDetail.checkSign(r.getInt("user_id"), r.getBigDecimal("money"), r.getBigDecimal("money_after"), r.getBigDecimal("money_before"), r.getInt("change_type"), r.getInt("change_ref_id"), r.getDate("create_time"), r.getStr("sign")))
			{
				r.set("is_valid", AppConst.STATUS_VAL_YES);
			}else{
				r.set("is_valid", AppConst.STATUS_VAL_NO);
			}
		}
		
		renderSuccess();
	}

}
